<?php
namespace App\Controller;
use Think\Controller;
class PlanController extends Controller {
    public function index(){
    	echo "PlanController";
    }

    public function createPlan(){
    	$plan = M('_plan');
        $data['userId'] = I('post.userId');
        $data['planName'] = I('post.planName');
        $data['planImage'] = I('post.planImage');
        $data['createTime'] = I('post.createTime');
        $data['endTime'] = I('post.endTime');
        $data['status'] = I('post.status');
    	// $data['userId'] = 16;
     //    $data['planName'] = '无敌风火轮';
     //    $data['planImage'] = 'plan_defualt';
     //    $data['createTime'] = '2018年3月11日';
     //    $data['endTime'] = '2018年3月20日';
     //    $data['status'] = '进行中';

        $res['code'] = $plan->add($data);
        if ($res['code']!=null) {
        	$res['msg'] = "创建计划成功";
    		$this->ajaxReturn($res,'JSON');
        }
    }

    public function getPlans($userId){
    	$plan = M('_plan');
    	$map['userId'] = $userId;
    	$res['plans'] = $plan->where($map)->order('createtime desc')->select();
    	if ($res['plans']!=null) {
    		$res['code'] = 2;
    		$res['msg'] = '获取plan数据成功';
    		$this->ajaxReturn($res,'JSON');
    	} else{
    		$res['code'] = 3;
    		$res['msg'] = '未添加plan';
    		$this->ajaxReturn($res,'JSON');
    	}
    }

    public function deletePlan($id){
    	$plan = M('_plan');
    	$map['id'] = $id;
    	$res = $plan->where($map)->delete();
    	if ($res != null) {
    		$response['code'] = 4;
    		$response['msg'] = '已删除';
    		$this->ajaxReturn($response,'JSON');
    	}
    }

    public function getPlanDetail($planId){
    	$planDetail = M('_plan_detail');
    	$map['planid'] = $planId;
    	$res['planDetails'] = $planDetail->where($map)->order('createtime asc')->select();
    	if($res['planDetails'] != null){
    		$res['msg'] = "获取数据成功";
    		$res['code'] = 2;
    	}else{
    		$res['msg'] = '无数据';
    		$res['code'] = 3;
    	}
    	$this->ajaxReturn($res,'JSON');
    }

    public function updatePlanDetail($planId,$id){
    	$planDetail = M('_plan_detail');
    	$map['planid'] = $planId;
    	$map['id'] = $id;
    	$data['content'] = I('post.content');
    	$data['createtime'] = I('post.createtime');
    	// $data['content'] = '我来试试';
    	// $data['createtime'] = '你是谁e~';

    	$res['id'] = $planDetail->where($map)->find();
    	if ($res['id'] == null) {
    		//说明加新的
    		$data['planid'] = $planId;
    		$res['id'] = $planDetail->add($data);
    		$response['code'] = 1;
    		$response['msg'] = '添加成功';
    		$this->ajaxReturn($response, 'JSON');
    	} else{
    		// 说明更新
    		$res = $planDetail->where($map)->save($data);
    		$response['code'] = 4;
    		$response['msg'] = "更新成功";
    		$this->ajaxReturn($response, 'JSON');
    	}
    }

    public function removePlanDetail($id){
    	$planDetail = M('_plan_detail')	;
    	$map['id'] = $id;
    	$res = $planDetail->where($map)->delete();
    	if ($res != null) {
    		$response['code'] = 4;
    		$response['msg'] = "已删除";
    		$this->ajaxReturn($response,'JSON');
    	}
    }

    public function checkClockIn($planId, $time){
    	$planClockIn = M('_plan_clock_in');
    	$map['planid'] = $planId;
    	$map['time'] = $time;
    	$res = $planClockIn->where($map)->find();
    //	echo M('_plan_clock_in')->getLastSql();
    	if($res == null){
    		$response['code'] = 4;
    		$response['msg'] = '未打卡';
    		$this->ajaxReturn($response,'JSON');
    	} else{
    		$response['code'] = -1;
    		$response['msg'] = '已打卡';
    		$this->ajaxReturn($response,'JSON');
    	}
    }

    public function clockIn($planId, $content, $remark, $time){
    	$planClockIn = M('_plan_clock_in');
    	$data['planid'] = $planId;
    	$data['content'] = $content;
    	$data['remark'] = $remark;
    	$data['time'] = $time;
    	$res = $planClockIn->add($data);
    	$response['code'] = 2;
    	$response['msg'] = "打卡成功";
    	$this->ajaxReturn($response,"JSON");
    }

    public function historyClockIn($planId){
    	$planClockIn = M('_plan_clock_in');
    	$map['planid'] = $planId;
    	$res['history'] = $planClockIn->where($map)->order('time desc')->select();
    	if ($res['history'] != null) {
    		$res['msg'] = '获取历史打卡数据';
    		$res['code'] = '2';
  			$this->ajaxReturn($res,"JSON");
    	} else{
    		$res['msg'] = "没有数据";
    		$res['code'] = '3';
    		$this->ajaxReturn($res,'JSON');
    	}
    }
}